﻿
#region License, Terms and Conditions
//
// User.cs
//
// Authors: Kori Francis <twitter.com/djbyter>
// Copyright (C) 2012 Kori Francis. All rights reserved.
// 
// THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#endregion

namespace Intercom.Core
{
    #region Imports
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using RestSharp.Deserializers;
    #endregion

    /// <summary>
    /// The paging view model
    /// </summary>
    public class UserViewModel
    {
        /// <summary>
        /// A list of User objects (same as returned by getting a single User)
        /// </summary>
        [DeserializeAs(Name="users")]
        public List<User> Users { get; set; }

        /// <summary>
        /// The total number of Users tracked in your Intercom application
        /// </summary>
        [DeserializeAs(Name = "total_count")]
        public int Total { get; set; }

        /// <summary>
        /// The current requested page
        /// </summary>
        [DeserializeAs(Name = "page")]
        public int Page { get; set; }

        /// <summary>
        /// The next page number, if any
        /// </summary>
        [DeserializeAs(Name = "next_page")]
        public int NextPage { get; set; }

        /// <summary>
        /// The previous page number, if any
        /// </summary>
        [DeserializeAs(Name = "previous_page")]
        public int PreviousPage { get; set; }

        /// <summary>
        /// The total number of pages
        /// </summary>
        [DeserializeAs(Name = "total_pages")]
        public int TotalPages { get; set; }
    }

    [DataContract]
    [JsonObject(MemberSerialization.OptIn)]
    public class User
    {
        /// <summary>
        /// The user's email address
        /// </summary>
        [DeserializeAs(Name = "email")]
        [JsonProperty("email")]
        public string Email { get; set; }

        /// <summary>
        /// A unique identifier for the user
        /// </summary>
        [DeserializeAs(Name = "user_id")]
        [JsonProperty("user_id")]
        public int Id { get; set; }

        /// <summary>
        /// The user's full name
        /// </summary>
        [DeserializeAs(Name = "name")]
        [JsonProperty("name")]
        public string Name { get; set; }

        /// <summary>
        /// The datetime the user was created
        /// </summary>
        [DeserializeAs(Name = "created_at")]
        [JsonProperty("created_at")]
        [JsonConverter(typeof(UnixJsonDateTimeConverter))]
        public DateTime CreatedAt { get; set; }

        [DeserializeAs(Name = "last_impression_at")]
        public DateTime LastImpressionAt { get; set; }

        /// <summary>
        /// A hash of key/value pairs containing any other data about the user you want Intercom to store.
        /// </summary>
        [DeserializeAs(Name = "custom_data")]
        [JsonProperty("custom_data")]
        public Dictionary<string, string> CustomData { get; set; }

        [DeserializeAs(Name = "social_profiles")]
        public List<SocialProfile> SocialProfiles { get; set; }

        [DeserializeAs(Name = "location_data")]
        public LocationData LocationData { get; set; }

        [DeserializeAs(Name = "session_count")]
        public int SessionCount { get; set; }

        /// <summary>
        /// An IP address (e.g. "1.2.3.4") representing the last ip address the user visited your application from. (Used for updating location_data)
        /// </summary>
        [DeserializeAs(Name = "last_seen_ip")]
        [JsonProperty("last_seen_ip")]
        public string LastSeenIP { get; set; }

        /// <summary>
        /// The user agent the user last visited your application with.
        /// </summary>
        [DeserializeAs(Name = "last_seen_user_agent")]
        [JsonProperty("last_seen_user_agent")]
        public string LastSeenUserAgent { get; set; }

        [DeserializeAs(Name = "relationship_score")]
        public int RelationshipScore { get; set; }
    }

    public class SocialProfile
    {
        [DeserializeAs(Name = "type")]
        public string Type { get; set; }

        [DeserializeAs(Name = "url")]
        public string Url { get; set; }

        [DeserializeAs(Name = "username")]
        public string Username { get; set; }

        // TODO: Weird, can't be int .. just too long.
        [DeserializeAs(Name = "id")]
        public string Id { get; set; }
    }

    public class LocationData
    {
        [DeserializeAs(Name = "city_name")]
        public string City { get; set; }

        [DeserializeAs(Name = "continent_code")]
        public string ContinentCode { get; set; }

        [DeserializeAs(Name = "country_name")]
        public string Country { get; set; }

        [DeserializeAs(Name = "latitude")]
        public double Latitude { get; set; }

        [DeserializeAs(Name = "longitude")]
        public double Longitude { get; set; }

        [DeserializeAs(Name = "postal_code")]
        public string PostalCode { get; set; }

        [DeserializeAs(Name = "region_name")]
        public string Region { get; set; }

        [DeserializeAs(Name = "timezone")]
        public string Timezone { get; set; }

        [DeserializeAs(Name = "country_code")]
        public string CountryCode { get; set; }
    }
}
